var express = require('express');
var router = express.Router();
const {Course,User,Category} = require('../models');
const { where } = require('sequelize');
const { failure, success } = require('../utils/response');

// 前台接口，此接口为首页展示数据接口，包括
router.get('/', async function(req, res, next) {
  try {
    // 推荐课程(前10个)查询
    const recommended = await Course.findAll({
      attributes:{exclude:['CategoryId','UserId','content']},
      include:[
        {
          model:Category,
          as:'category',
          attributes:['id','name']
        },
        {
          model:User,
          as:'user',
          attributes:['id','username','nickname','avatar','company']
        },
      ],
      where:{recommended:true},
      order:[['id','desc']],
      limit:10
    })
    // 人气(点赞量前10)课程查询
    const likesCourses = await Course.findAll({
      attributes:{exclude:['CategoryId','UserId','content']},
      order:[['likesCount','desc'],['id','desc']],
      limit:10
    })
    // 人气(点赞量前10)课程查询
    const introductoryCourses = await Course.findAll({
      attributes:{exclude:['CategoryId','UserId','content']},
      where:{introductory:true},
      order:[['id','desc']],
      limit:10
    })
    success(res,'获取首页数据成功',{
      recommended,
      likesCourses,
      introductoryCourses
    })
  } catch (error) {
    failure(res,error)
  }
});

module.exports = router;
